﻿using Microsoft.AspNetCore.Mvc;
using ManageSystem.Model;
using System.Threading.Tasks;
using ManageSystem.Service;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;


namespace ManageSystem.Controllers
{
    /// <summary>
    /// ADD and manage outlaystream
    /// </summary>
    [Route("ManageSystem/[controller]/[action]")]
    [ApiController]
    [Produces("application/json")]
    public class OutlayStreamController : ControllerBase
    {
        public ProjectService projectService = new();
        public OutlayStreamService outlayStreamService = new();

        ///<summary>
        ///添加某个项目的支出
        ///</summary>
        ///<param name="outlayStream"></param>
        ///<returns></returns>
        [HttpPost]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> AddOutlayStream(OutlayStream outlayStream)
        {
            // 当前项目
            Project projectToBeUpdated = await projectService.QueryByPrimaryKey(outlayStream.projectGuid);

            // 插入项目的支出流水，并且返回处理以后的项目让service插入到数据库
            Project projectWillBeUpdated = await outlayStreamService.DealWithOutlayStreamsOfProject(outlayStream, projectToBeUpdated, "add");

            // 更新数据
            bool isUpdated = await projectService.Update(projectWillBeUpdated);

            if (isUpdated)
            {
                return Ok("添加支出流水成功!已更新项目支出信息");
            }
            else
            {
                return Ok("操作失败！");
            }
        }

        ///<summary>
        ///根据主键删除某个项目的支出流水
        ///</summary>
        ///<param name="streamID"></param>
        ///<returns></returns>
        [HttpDelete]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> DeleteOutlayStream(int streamID)
        {
            // 当前项目
            OutlayStream outlayStream = await outlayStreamService.QueryByPrimaryKey(streamID);
            Project projectToBeUpdated = await projectService.QueryByPrimaryKey(outlayStream.projectGuid);
            // 改变流水后要被更新到数据库的项目
            Project projectWillBeUpdated = await outlayStreamService.DealWithOutlayStreamsOfProject(outlayStream, projectToBeUpdated, "delete");
            // 更新项目到数据库
            bool isSuccess = await projectService.Update(projectWillBeUpdated);

            if (isSuccess)
            {
                return Ok("删除支出流水成功");
            }
            else
            {
                return BadRequest("删除支出流水失败");
            }
        }

        ///<summary>
        ///查询数据库全部支出流水
        ///</summary>
        ///<returns></returns>
        [HttpGet]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> QueryAllOutlayStream()
        {
            List<OutlayStream> outlayStreams = await outlayStreamService.QueryAll();
            if (outlayStreams.Count == 0)
            {
                return NotFound("当前数据库暂无支出流水！");
            }
            else
            {
                return Ok(outlayStreams);
            }
        }

        ///<summary>
        ///根据projectGuid查询某个项目的支出流水
        ///</summary>
        ///<param name="projectGuid"></param>
        ///<returns></returns>
        [HttpGet]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> QueryOutlayStreamByProjectGuid(string projectGuid)
        {
            List<OutlayStream> outlayStreams = await outlayStreamService.QueryOutlayStreamByProjectGuid(projectGuid);
            if (outlayStreams.Count == 0)
            {
                return NotFound("当前项目暂未创建支出流水！");
            }
            else
            {
                return Ok(outlayStreams);
            }
        }

        ///<summary>
        ///更新某个项目的支出流水
        ///</summary>
        ///<param name="outlayStream"></param>
        ///<returns></returns>
        [HttpPut]
        [Authorize(Policy = "Permission")]
        public async Task<IActionResult> UpdateOutlayStream(OutlayStream outlayStream)
        {
            // 当前项目
            Project projectToBeUpdated = await projectService.QueryByPrimaryKey(outlayStream.projectGuid);

            // 更新项目的支出，并且返回处理以后的项目让service插入到数据库
            Project projectWillBeUpdated = await outlayStreamService.DealWithOutlayStreamsOfProject(outlayStream, projectToBeUpdated, "update");

            // 更新数据
            bool isUpdated = await projectService.Update(projectWillBeUpdated);

            if (isUpdated)
            {
                return Ok("更新支出流水成功!已将信息同步更新到项目中");
            }
            else
            {
                return BadRequest("操作失败！");
            }
        }
    }
}
